package com.jing21.learn.basic.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jing21.learn.basic.mapper.ProductMapper;
import com.jing21.learn.common.learn.PageQuery;
import com.jing21.learn.intf.entity.Product;
import com.jing21.learn.intf.feignclients.ProductClient;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Objects;

/**
 * Created by 郑靖 on 2017/9/22.
 */
@RestController
@Api(value = "ProductController", description = "商品")
@RequestMapping("/product")
public class ProductController implements ProductClient {

    @Autowired
    ProductMapper productMapper;

    @Override
    public boolean saveOrUpdate(@RequestBody Product product) {
        if (Objects.isNull(product.getId())) {
            product.setCreateTime(new Date());
            product.setCreator(1);
            return productMapper.insertUseGeneratedKeys(product) > 0;
        }
        product.setEditor(1);
        product.setEditTime(new Date());
        return productMapper.updateByPrimaryKeySelective(product) > 0;
    }

    @Override
//    @Cacheable(keyGenerator = "")
    public PageInfo<Product> selectByPage(@RequestBody PageQuery<Product> query) {

        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> productMapper.select(query.getQuery()));

    }
}
